package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"goPlay/dams"
	"goPlay/logger"
	"io/ioutil"
	"net/http"
	"runtime"
)

func main() {
	dams.Register()
	logger.SetRollingDaily(".", "test.log")
	logger.SetLevel(logger.DEBUG)
	logger.Info("register log config done")
	runtime.GOMAXPROCS(runtime.NumCPU())
	http.HandleFunc("/test", test)

	http.HandleFunc("/test_struct", test_struct)

	http.HandleFunc("/test_redis_cache", test_redis_cache)
	logger.Info("start service")
	err := http.ListenAndServe(":9090", nil)
	if err != nil {
		logger.Fatal("listenandserve", err)
	}
	logger.Info("END")
}

func test(w http.ResponseWriter, r *http.Request) {
	logger.Info("Request Result")
	fmt.Fprintln(w, "success")
}

func test_struct(w http.ResponseWriter, r *http.Request) {
	body, _ := ioutil.ReadAll(r.Body)
	jsonstr := bytes.NewBuffer(body).String()
	logger.Info("Get GetQuestionDouble Request:", jsonstr)
	var data GetQuestion
	if err := json.Unmarshal([]byte(jsonstr), &data); err == nil {
		for _, studentQue := range data.StudentInfo {
			dams.ComeHere(studentQue.Studentid)
		}

		fmt.Fprintln(w, "success test_struct")
	} else {
		fmt.Fprintln(w, "fail test_struct")
	}
}

func test_redis_cache(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "success test_redis_cache")
}

type StudentQueue struct {
	Studentid int
	Stage     int
}

type GetQuestion struct {
	StudentInfo []StudentQueue
	StartTime   string
}
